// Created: Dec 11, 2014
//
// Author: wjinwen.1988@gmail.com

#ifndef CHI_THREAD_H
#define CHI_THREAD_H

#include <pthread.h>

#include <string>
#include <functional>

namespace chi {

class Thread {
 public:
  using ThreadFunc = std::function<void ()>;
  explicit Thread(const ThreadFunc& func, 
                  const std::string& name = std::string());
  ~Thread();

  Thread(const Thread&) = delete;
  void operator=(const Thread&) = delete;

  void Start();
  int Join();

  bool started() const { return started_; }
 private:
  static void* StartThread(void *arg);
  void RunInThread();

  bool started_;
  bool joined_;
  pthread_t pthreadid_;
  ThreadFunc func_;   // 线程执行回调函数
  std::string name_;  // 线程名称 
};

}  // namespace chi
#endif
